package com.telecomyt.jwportal.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.utils.GlideUtils;
import com.telecomyt.jwportal.utils.ToastUtils;

/**
 * @author created by chengDa
 * @time 2020/8/26  9:45
 * @description
 */
public class DialogUtils extends Dialog implements View.OnClickListener {

    private TextView messageTextView;
    private final  int isSelect;
    private TextView mCancel;
    private TextView mConfirm;
    private String mTitle, mStrCancle, mStrConfirm;
    private String mShareName, mIconPath, mShareContentPath;
    private EditText mEditText;
    private String imagePath;//加载一张大图时的图片资源
    private String callName;//呼叫姓名

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: isSelect=1 文本加两个按钮样式 isSelect =2 分享模板 isSelect=3 文本输入框样式 isSelect=4 图片样式
     */
    public DialogUtils(@NonNull Context context, int isSelect) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: message标题 cancle 取消按钮文本 confirm 确认按钮文本
     */
    public DialogUtils(@NonNull Context context, int isSelect, String title, String cancle, String confirm, ConfirmListener listener) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
        this.mTitle = title;
        this.mStrConfirm = confirm;
        this.mStrCancle = cancle;
        this.listener = listener;
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: shareName 分享人 shareIcon 分享头像 cancle 取消按钮文本 confirm 确认按钮文本
     */
    public DialogUtils(@NonNull Context context, int isSelect, String shareName, String shareHead, String shareContentPath, String cancle, String confirm, ConfirmListener listener) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
        this.mShareName = shareName;
        this.mIconPath = shareHead;
        this.mShareContentPath = shareContentPath;
        this.mStrCancle = cancle;
        this.mStrConfirm = confirm;
        this.listener = listener;
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: cancle 取消按钮文本 confirm 确认按钮文本
     */
    public DialogUtils(@NonNull Context context, int isSelect, String cancle, String confirm, ModificationListener listener) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
        this.mStrCancle = cancle;
        this.mStrConfirm = confirm;
        this.modificationListener = listener;
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: cancle 取消按钮文本 confirm 确认按钮文本
     */
    public DialogUtils(@NonNull Context context, int isSelect, String callText, CallListener listener) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
        this.callName = callText;
        this.callListener = listener;
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: imagePath 图片资源
     */
    public DialogUtils(@NonNull Context context, int isSelect, String imagePath) {
        super(context, R.style.DialogStyle);
        this.isSelect = isSelect;
        this.imagePath = imagePath;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        switch (isSelect) {
            case 1:
                setContentView(R.layout.layout_dialog_text);
                messageTextView = (TextView) findViewById(R.id.mTvMessage);
                messageTextView.setText(mTitle);
                setBottomButtonText();
                break;
            case 2:
                setContentView(R.layout.layout_dialog_share);
                ImageView mIvShareHead = (ImageView) findViewById(R.id.mIvShareHead);
                ImageView mIvShareContent = (ImageView) findViewById(R.id.mIvShareContent);
                TextView mTvShareName = (TextView) findViewById(R.id.mTvShareName);
//                GlideUtils.load(getContext(), mIconPath, mIvShareHead);//加载头像
                GlideUtils.load(getContext(), mShareContentPath, mIvShareContent);//加载分享图片
                mTvShareName.setText(mShareName);
                setBottomButtonText();
                break;
            case 3:
                setContentView(R.layout.layout_dialog_edit);
                mEditText = findViewById(R.id.mEditText);
                setBottomButtonText();
                break;
            case 4:
                setContentView(R.layout.layout_dialog_image);
                ImageView mImageView = findViewById(R.id.mImageView);
                LinearLayout mLlCancle = findViewById(R.id.mLlCancle);
                GlideUtils.load(getContext(), imagePath, mImageView);//加载头像
                mLlCancle.setOnClickListener(this);
                break;
            case 5:
                setContentView(R.layout.layout_dialog_call);
                TextView mTvTitle = findViewById(R.id.mTvTitle);
                TextView mTvHangUp = findViewById(R.id.mTvHangUp);
                mTvHangUp.setOnClickListener(this);
                mTvTitle.setText(String.format("正在呼叫%s,请稍后...", callName));
                break;
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.mTvConfirm:
                if (isSelect == 3) {
                    if (TextUtils.isEmpty(mEditText.getText().toString().trim())) {
                        ToastUtils.showShort("不能为空!");
                        return;
                    }
                    if (modificationListener != null) {
                        modificationListener.onConfirmName(mEditText.getText().toString().trim());
                    }
                    dismiss();
                } else {
                    if (listener != null) {
                        listener.onConfirm();
                    }
                    dismiss();
                }

                break;
            case R.id.mLlCancle:
            case R.id.mTvCancel:
                dismiss();
                break;
            case R.id.mTvHangUp:
                if (callListener != null) {
                    callListener.onHangUp();
                }
                dismiss();
                break;
        }

    }

    @Override
    public void show() {
        super.show();

    }

    private ConfirmListener listener;

    public interface ConfirmListener {
        void onConfirm();

    }

    private ModificationListener modificationListener;

    public interface ModificationListener {
        void onConfirmName(String name);
    }

    private CallListener callListener;

    public interface CallListener {
        void onHangUp();
    }

    /**
     * @date: 2020/8/26
     * @author: chengDa
     * @description: 设置底部两个按钮赋值监听
     */
    private void setBottomButtonText() {
        mCancel = (TextView) findViewById(R.id.mTvCancel);
        mConfirm = (TextView) findViewById(R.id.mTvConfirm);
        mCancel.setText(mStrCancle);
        mConfirm.setText(mStrConfirm);
        mConfirm.setOnClickListener(this);
        mCancel.setOnClickListener(this);
    }



}
